(asdf:operate 'asdf:load-op :ht-simple-ajax)
(defpackage :accounts
  (:use :ht-simple-ajax :http-server :cl)
  (:export :check-username :check-password :register-user :check-logged-in-session))

(defparameter *config-path* "./data-store/")

(in-package :accounts)

(ht-simple-ajax:defun-ajax login-user (username password) (http-server:tbg-ajax-processor)
  (if (string= (check-username username) "true")
      (let ((userdata (read-user-data username)))
	(let ((read-password (cadr userdata)))
	  (print username)
	  (if (string= read-password password)
	      (progn
	       (setf user-session (hunchentoot:start-session))
	       (setf (hunchentoot:session-value :WORKED) 1000)
	       (setf (hunchentoot:session-value :LOGGED-IN) t)
	       (return-from login-user "true"))
	      (return-from login-user "false"))))
      (return-from login-user "false")))

(ht-simple-ajax:defun-ajax check-username (username) (http-server:tbg-ajax-processor)
  (if (probe-file (concatenate 'string *config-path* "users/" (princ-to-string username) ".lsp"))
  ;;; if file does not exist we return false meaning that there is no such username yet. If file exists we signal to the page that such user is already present
      (princ-to-string "true")
      (princ-to-string "false")))
      
(ht-simple-ajax:defun-ajax check-password (password) (http-server:tbg-ajax-processor)
    (if (> (length password) 4)
	(princ-to-string "true")
	(princ-to-string "false")))

(ht-simple-ajax:defun-ajax register-user (username password email firstname lastname) (http-server:tbg-ajax-processor)
  (if (string= (check-username username) "true")
      (return-from register-user "false"))
  (if (string= (check-password password) "false")
      (return-from register-user "false"))
  (if (string= (princ-to-string password) "")
      (return-form register-user "false"))
  (if (string= (princ-to-string email) "")
      (return-form register-user "false"))
  (if (string= (princ-to-string firstname) "")
      (return-form register-user "false"))
  (if (string= (princ-to-string lastname) "")
      (return-form register-user "false"))
  (write-user-data username password email firstname lastname)
  (return-from register-user "true"))

(defun write-user-data (username password email firstname lastname)
  (let ((user-filename (concatenate 'string *config-path* "users/" username ".lsp")) (username-object `(,username ,password ,email ,firstname ,lastname)))
    (with-open-file (user-stream user-filename :direction :output)
      (prin1 username-object user-stream))))

(defun read-user-data (username)
  (if (probe-file (concatenate 'string *config-path* "users/" (princ-to-string username) ".lsp"))

    (let ((user-filename (concatenate 'string *config-path* "users/" username ".lsp")))
      (with-open-file (user-stream user-filename :direction :input)
	(return-from read-user-data (read user-stream))))
    (return-from read-user-data nil)))

(defun check-logged-in-session ()
  (If (hunchentoot:session-value :LOGGED-IN)
      (return-from check-logged-in-session t)
      (return-from check-logged-in-session nil)))
